package com.gict.antifraudback.controller.web.article;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gict.antifraudback.entity.Article;
import com.gict.antifraudback.entity.ArticleComment;
import com.gict.antifraudback.entity.User;
import com.gict.antifraudback.exception.MyException;
import com.gict.antifraudback.result.ResultEnum;
import com.gict.antifraudback.result.ResultView;
import com.gict.antifraudback.service.ArticleCommentService;
import com.gict.antifraudback.service.ArticleService;
import com.gict.antifraudback.service.UserService;
import com.gict.antifraudback.vo.ArticleCommentVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ThinkPad
 */
@RestController
@Slf4j
@RequestMapping("/wapi/articleComment")
public class ArticleCommentController {

    @Autowired
    private ArticleCommentService articleCommentService;
    @Autowired
    private ArticleService articleService;
    @Autowired
    private UserService userService;

    @GetMapping("/getList")
    public ResultView getList(ArticleCommentVo articleCommentVo) {
        LambdaQueryWrapper<ArticleComment> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(articleCommentVo.getUserId() != null, ArticleComment::getUserId, articleCommentVo.getUserId());
        wrapper.eq(articleCommentVo.getArticleId() != null, ArticleComment::getArticleId, articleCommentVo.getArticleId());
        IPage<ArticleComment> page = articleCommentService.page(new Page<>(articleCommentVo.getCurrent(), articleCommentVo.getPageSize()), wrapper);
        List<ArticleComment> records = page.getRecords();
        if (CollectionUtils.isNotEmpty(records)) {
            // 需要查询出关联的文章标题
            List<Integer> articleIds = records.stream().map(ArticleComment::getArticleId).collect(Collectors.toList());
            Collection<Article> articles = articleService.listByIds(articleIds);
            if (CollectionUtils.isNotEmpty(articles)) {
                Map<Integer, String> map = articles.stream().collect(Collectors.toMap(Article::getId, Article::getArticleTitle));
                for (ArticleComment record : records) {
                    record.setArticleTitle(map.get(record.getArticleId()));
                }
            }

            // 需要查询出关联的用户
            List<Integer> userIds = records.stream().map(ArticleComment::getUserId).collect(Collectors.toList());
            Collection<User> users = userService.listByIds(userIds);
            if (CollectionUtils.isNotEmpty(users)) {
                Map<Integer, User> map = users.stream().collect(Collectors.toMap(User::getId, a -> a));
                for (ArticleComment record : records) {
                    User user = map.get(record.getUserId());
                    if (user != null){
                        record.setUserEmail(user.getEmail());
                        record.setUserName(user.getName());
                        record.setUserAvatar(user.getAvatar());
                    }
                }
            }
        }
        return ResultView.ok(page);
    }

    @GetMapping("/get")
    public ResultView get(@Valid @NotNull Integer id) {
        ArticleComment articleComment = articleCommentService.getById(id);
        if (articleComment == null) {
            throw new MyException("评论不存在");
        }
        return ResultView.ok(articleComment);
    }

    @PostMapping("/delete")
    public ResultView delete(@RequestBody ArrayList<Integer> ids) {
        log.info("articleCommentVo:{}", ids);
        for (Integer id : ids) {
            articleCommentService.removeById(id);
        }
        return ResultView.okMsg("删除成功");
    }

    @PostMapping("/add")
    public ResultView add(@Valid @RequestBody ArticleComment articleComment) {
        articleCommentService.save(articleComment);
        return ResultView.ok();
    }

    @PostMapping("/update")
    public ResultView update(@RequestBody @Valid ArticleComment articleComment) {
        ArticleComment update = articleCommentService.getById(articleComment.getId());
        if (update == null) {
            return ResultView.error(ResultEnum.CODE_2, "数据不存在！");
        }
        articleCommentService.updateById(articleComment);
        return ResultView.ok();
    }
}
